﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSNPro.Base;
using TSNPro.Device;
using TSNPro.OperationControl.Command;

namespace TSNPro.OperationControl
{
    public class BaseController : BaseItem
    {
        private List<BaseCommand> _allCommands = new List<BaseCommand>();
        //private NotificationManager _notificationManager = NotificationManager.Instance;

        public BaseController()
        {
        }

        public List<string> GetCommandNames()
        {
            return _allCommands.Select(cmd => cmd.Name).ToList();
        }

        public List<BaseCommand> GetCommands()
        {
            return _allCommands;
        }

        public BaseCommand GetCommand(string name)
        {
            var cmd = _allCommands.FirstOrDefault(c => c.Name == name);
            if (cmd == null)
            {
                throw new InvalidOperationException($"BaseController::GetCommand 未找到{name}指令!");
            }
            return cmd;
        }

        public void ExecuteWrap(BaseCommand cmd)
        {
            var ela = Stopwatch.StartNew();
            Console.WriteLine($"--- {cmd.Name} 开始运行");
            WaitMsNoProgress(10);
            cmd.Execute();
            WaitMsNoProgress(10);
            Console.WriteLine($"--- 指令执行完毕 {ela.Elapsed.TotalSeconds}s");
        }

        public void ExecuteWrap(List<BaseCommand> cmds)
        {
            CheckWeightSum(cmds, 100);
            var ela = Stopwatch.StartNew();
            Console.WriteLine($"总流程开始: {cmds.Count} command");
            //_notificationManager.Reset();

            foreach (var curCmd in cmds)
            {
                //curCmd.ProgressChanged += (sender, e) =>
                //{
                //    _notificationManager.SetCurrentFlow(e.Title);
                //    _notificationManager.UpdateProgress(e.Value);
                //};

                int weight = curCmd.GetWeight();
                if (curCmd.GetSubCommands().Count == 0)
                {
                    //_notificationManager.AddBaseProgress(weight);
                }

                //_notificationManager.SetCurrentFlow(curCmd.FriendlyName);
                ExecuteWrap(curCmd);
                //_notificationManager.UpdateProgress(100);
            }

            Console.WriteLine($"总流程结束 {ela.Elapsed.TotalSeconds}s");
        }

        public void CheckWeightSum(List<BaseCommand> cmds, int sumWeight)
        {
            int sum = cmds.Sum(it => it.GetWeight());
            Console.WriteLine($"{nameof(CheckWeightSum)}: {sum}");
            if (sum != sumWeight)
            {
                throw new InvalidOperationException($"所有指令权重总错误! 当前: {sum} 目标: {sumWeight}");
            }
        }

        public void Stop()
        {
            //DeviceManager.GetInstance().Stop();
        }

        private void WaitMsNoProgress(int ms)
        {

        }

        //public override System.Windows.Forms.Control GetConfigWidget()
        //{
        //    var w = new DialogControl();
        //    w.SetController(this);
        //    return w;
        //}
    }

}
